package com.example.teatea.controller;

import com.example.teatea.dao.CheckGood;
import com.example.teatea.dao.CheckNum;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;

public class GoodMessageController implements Initializable {
    public TextField name;
    public TextField price;
    public Button sureBt;
    public Button candleBt;
    public ImageView photo;
    public ComboBox typeBox;
    public TextField number;

    public void toCancle(ActionEvent actionEvent) {
        Stage stage = (Stage) candleBt.getScene().getWindow();
        stage.close();
    }

    public void toSure(ActionEvent actionEvent) {
        SeatController.hashMap.put(OrderController.good, Integer.valueOf(number.getText()));
        Stage stage = (Stage) sureBt.getScene().getWindow();
        stage.close();
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        name.setText(OrderController.good.getgName());
        price.setText(String.valueOf(OrderController.good.getgPrice()));
        typeBox.setValue(OrderController.good.getgType());
        try {
            Image image = new Image(new FileInputStream("E:\\TeaTea\\src\\main\\resources\\com\\example\\teatea\\photo\\food\\" + OrderController.good.getgImage()));
            photo.setImage(image);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if(SeatController.hashMap.get(name.getText()) != null) number.setText(String.valueOf(SeatController.hashMap.get(name.getText())));
        number.focusedProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean t1) {
                if(aBoolean && !t1){
                    int num = Integer.parseInt(number.getText());
                    if(new CheckNum().checkNum(name.getText()) < num){
                        Alert alert=new Alert(Alert.AlertType.INFORMATION);
                        alert.setHeaderText("该商品库存不足，请减少数量！");
                        alert.show();
                        number.requestFocus();
                    }
                }
            }
        });
    }
}
